package com.gxa.shop.servlet.product;


import com.gxa.shop.bean.dto.ProductQueryPage;

import com.gxa.shop.bean.vo.ProductOption;
import com.alibaba.fastjson.JSON;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.service.prodect.ProductService;
import com.gxa.shop.service.prodect.impl.ProductServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Collections;
import java.util.List;

/**
 * com.gxa.shop.bean.servlet.product
 * User: zjy
 * Date: 2024/12/19 15:52
 * motto:   逆水行舟不进则退
 * Description:
 * Version: V1.0
 */
@WebServlet(name = "ProductFindPage",value = "/product/findPage")
public class ProductFindPage extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        // 获取并验证请求参数
        String str_page = req.getParameter("page");
        String str_limit = req.getParameter("limit");
        String categoryName=req.getParameter("categoryName");
        String brandName=req.getParameter("brandName");

        String productName=req.getParameter("productName");

        String productColor=req.getParameter("productColor");

        String productMaterial=req.getParameter("productMaterial");

        String launchTimeStr = req.getParameter("launchTime");
        LocalDate launchTime = null;
        if (launchTimeStr != null && !launchTimeStr.trim().isEmpty()) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            try {
                launchTime = LocalDate.parse(launchTimeStr, formatter);
            } catch (DateTimeParseException e) {
                // 处理解析异常
                e.printStackTrace();
                // 可以选择设置默认值或者返回错误信息给客户端
            }
        }

        // 创建查询实体

        ProductQueryPage productQueryPage = new ProductQueryPage();

        // 判断是否有参数并处理
        if (str_page != null && !str_page.trim().isEmpty()) {
            try {
                productQueryPage.setPage(Integer.parseInt(str_page.trim()));
            } catch (NumberFormatException e) {
                productQueryPage.setPage(1);
            }
        }

        if (str_limit != null && !str_limit.trim().isEmpty()) {
            try {
                productQueryPage.setLimit(Integer.parseInt(str_limit.trim()));
            } catch (NumberFormatException e) {
                productQueryPage.setLimit(5);
            }
        }
        productQueryPage.setProductName(productName);
        productQueryPage.setProductColor(productColor);
        productQueryPage.setProductMaterial(productMaterial);
        productQueryPage.setBrandName(brandName);
        productQueryPage.setCategoryName(categoryName);

        // 初始化服务层对象
        ProductService productService = new ProductServiceImpl();

        // 满足条件的总条数
        Long total = productService.findByTotal(productQueryPage);

        // 当前页码的数据，默认为空列表
        List<ProductOption> list = Collections.emptyList();
        if (total > 0) {
            list = productService.findByPage(productQueryPage);
        }

        // 构建响应数据
        R r = R.ok().addData("total", total).addData("list", list);

        // 响应对象
        PrintWriter writer = resp.getWriter();
        String result = JSON.toJSONString(r);
        System.out.println("FindByPageServlet: " + result);
        writer.write(result);
        writer.flush();
        writer.close();
    }
}
